package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.PageResult;
import com.pinyougou.mapper.SellerMapper;
import com.pinyougou.pojo.Seller;
import com.pinyougou.service.SellerService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SellerServiceImpl implements SellerService
{
    @Autowired
    private SellerMapper sellerMapper;

    /**
     * 多条件分页查询
     *
     * @param seller
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageResult findByPage(Seller seller, int pageNum, int pageSize)
    {
        PageInfo<Seller> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(new ISelect()
        {
            @Override
            public void doSelect()
            {
                sellerMapper.findByPage(seller);
            }
        });
        return new PageResult(pageInfo.getPages(),pageInfo.getList());
    }

    @Override
    public boolean updateStatus(Seller seller)
    {

        try
        {
            sellerMapper.updateByPrimaryKeySelective(seller);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 添加方法
     *
     * @param seller
     */
    @Override
    public void save(Seller seller)
    {
        seller.setStatus("0");
        seller.setCreateTime(new Date());
        //密码加密
        String password = new SimpleHash("md5", seller.getPassword(), seller.getSellerId(), 5).toHex();
        seller.setPassword(password);
        sellerMapper.insertSelective(seller);
    }

    /**
     * 修改方法
     *
     * @param seller
     */
    @Override
    public void update(Seller seller)
    {

    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id)
    {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids)
    {

    }

    /**
     * 根据主键id查询
     *
     * @param sellerId
     */
    @Override
    public Seller findOne(Serializable sellerId)
    {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    /**
     * 查询全部
     */
    @Override
    public List<Seller> findAll()
    {
        return null;
    }


}